package pri.zjy.interview;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangjy
 * @description
 * @date 2025/6/6 13:26
 */
public class Test6 {

    private volatile static Object singleton;

    private Test6(){}

    // 总次数
    private static int count = 0;
    private static AtomicInteger atomicInteger = new AtomicInteger();

    private static ConcurrentHashMap<String, Integer> sourceMap = new ConcurrentHashMap<>();

    public static Object getInstance() {
        // 加锁统计次数
        synchronized (Test6.class) {
            count++;
            // 每个来源的次数
            if (sourceMap.containsKey(Thread.currentThread().getName())) {
                sourceMap.put(Thread.currentThread().getName(), sourceMap.get(Thread.currentThread().getName()) + 1);
            } else {
                sourceMap.put(Thread.currentThread().getName(), sourceMap.getOrDefault(Thread.currentThread().getName(), 0) + 1);
            }
        }

        // 懒加载
        // 双夹
        if (singleton == null) {
            // 加锁
            synchronized (Test6.class) {
                // 二次判断
                if (singleton == null) {
                    singleton = new Object();
                }
            }
        }
        return singleton;
    }

}
